package com.cuitrip.business.home.recommend;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuitrip.apiservice.TripBusiness;
import com.cuitrip.business.home.recommend.model.Category;
import com.cuitrip.business.home.recommend.model.HomeCardType;
import com.cuitrip.business.home.travel.model.SelectInfo;
import com.cuitrip.component.statelayout.StateLayout;
import com.cuitrip.service.R;
import com.lab.app.BaseFragment;
import com.lab.component.list.CTRecyclerView;
import com.lab.component.list.simple.CTSimpleRecyclerAdapter;
import com.lab.component.list.simple.CTSimpleViewHolder;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.schemeurl.SchemeUrlHandler;
import com.lab.schemeurl.url.UrlBuilder;
import com.lab.utils.ImageHelper;
import com.lab.utils.MessageUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class RecommendCategoryFragment extends BaseFragment implements IProxyCallback {

    @Bind(R.id.ct_recycler_view)
    CTRecyclerView mCTRecyclerView;
    CTSimpleRecyclerAdapter<Category> mAdapter;
    @Bind(R.id.state_layout)
    StateLayout mStateLayout;
    private View mContentView;
    private List<Category> categoryList = new ArrayList<>();

    private ApiProxy mApiProxy = new ApiProxy(this);

    /**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.ct_layout_category_list, container, false);
        ButterKnife.bind(this, mContentView);
        initView();
        initListener();
        initData();
        return mContentView;
    }


    private void initView() {

        GridLayoutManager mgr = new GridLayoutManager(getHostActivity(), 2);
        mCTRecyclerView.setLayoutManager(mgr);
        mAdapter = new CTSimpleRecyclerAdapter<Category>(getHostActivity(), categoryList) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.ct_layout_category_item;
            }

            @Override
            public void onNewViewHolder(CTSimpleViewHolder viewHolder, ViewGroup parent, int viewType) {
            }

            @Override
            public void bindData(CTSimpleViewHolder holder, int position, final Category item) {
                ImageView imageView = holder.getView(R.id.category_logo);
                ImageHelper.displayCtImage(item.getImgUrl(), imageView, null);

                TextView titleView = holder.getView(R.id.category_title);
                titleView.setText(item.getName());

                View contentLayout = holder.getView(R.id.content_layout);
                contentLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UrlBuilder urlBuilder = new UrlBuilder(item.getUrl());
                        urlBuilder.addObject(SelectInfo.TITLE_KEY, item.getName());
                        urlBuilder.addObject("type", String.valueOf(HomeCardType.SUBJECT_SEARCH.value));
                        SchemeUrlHandler.getInstance().dispatch(getHostActivity(), Uri.parse(urlBuilder.toUrl()));
                    }
                });

            }
        };
        mCTRecyclerView.setAdapter(mAdapter);


    }

    private void initListener() {

    }

    private void initData() {
        TripBusiness.getRecommendCategoryList(mApiProxy);

    }

    private void inflateList(List<Category> categoryList) {
        this.categoryList.clear();
        this.categoryList.addAll(categoryList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        if (!isAdded()) {
            return false;
        }
        CtApiResponse response = (CtApiResponse) result.getData();
        if (response.isResponseNormal()) {
            if (response.result instanceof List) {
                mStateLayout.changeState(StateLayout.State.INIT);
                List<Category> categoryList = (List<Category>) response.result;
                inflateList(categoryList);
            }
        } else {
            if (response != null && !TextUtils.isEmpty(response.msg)) {
                MessageUtils.showToast(response.msg);
            }
            mStateLayout.changeState(StateLayout.State.INIT);
        }
        return false;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
